package com.jz.digital.main

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.jz.digital.R
import com.jz.digital.adapter.HomeEntranceAdapter
import com.jz.digital.base.BaseActivity
import com.jz.digital.bean.UserInfo
import com.jz.digital.caigou_shpiuliao.CaiGouShouLiaoActivity
import com.jz.digital.generate_get.MaterialRequisitionCommitActivity
import com.jz.digital.gongxu.GongXuCommitActivity
import com.jz.digital.login.LoginActivity
import com.jz.digital.utils.AccountManagerHelper
import com.jz.digital.utils.LogUtils
import com.jz.digital.utils.systembar.ImmersionBar
import com.jz.digital.warehousing.ProductionWarehousingActivity
import com.jz.digital.xiaoshou_chuku.XiaoShouChuKuActivity
import kotlinx.android.synthetic.main.activity_main.ov_about
import kotlinx.android.synthetic.main.activity_main.ov_exit_login
import kotlinx.android.synthetic.main.activity_main.recycle_view
import kotlinx.android.synthetic.main.activity_main.tv_title

/**
 * ================================================
 * Created: WangYong
 * Date: 2024/11/19
 * Description:
 * ================================================
 */
class MainActivity : BaseActivity<MainPresenter>(), IMainView, View.OnClickListener {
    private var mEntranceList = mutableListOf<UserInfo.PermissionBean>()
    private val mAdapter: HomeEntranceAdapter by lazy {
        HomeEntranceAdapter(mEntranceList)
    }

    private var lastOnBackTime = 0L
    override fun initData() {
        super.initData()
        initAdapter()
        mPresenter.initConfig()
    }

    override fun initViews() {
        super.initViews()
        ImmersionBar.with(this).statusBarColor(R.color.main_color)
            .titleBarMarginTop(tv_title)
            .init()
        setTitle(R.string.mobile_office)
    }

    override fun initListening() {
        super.initListening()
        ov_exit_login.setOnClickListener(this::onClick)
        ov_about.setOnClickListener(this::onClick)
    }

    /**
     * 初始化适配器
     */
    private fun initAdapter() {
        recycle_view.layoutManager = LinearLayoutManager(this)
        recycle_view.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val data: UserInfo.PermissionBean = adapter.data[position] as UserInfo.PermissionBean
            when (data.featuresId) {
                UserInfo.PURCHASING_AND_RECEIVING_MATERIALS -> {
                    //采购收料
                    CaiGouShouLiaoActivity.startActivity(this)
                }

                UserInfo.FINISHED_PRODUCT_WAREHOUSING -> {
                    //生产入库
                    ProductionWarehousingActivity.startActivity(this)
                }

                UserInfo.PRODUCTION_MATERIAL_REQUISITION -> {
                    //生产领料
                    MaterialRequisitionCommitActivity.startActivity(this)
                }

                UserInfo.PROCESS_REPORT -> {
                    //工序汇报
                    GongXuCommitActivity.startActivity(this)
                }

                UserInfo.FINISHED_PRODUCT_SHIPMENT -> {
                    //销售出库
                    XiaoShouChuKuActivity.startActivity(this)
                }
            }
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_main
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.ov_exit_login -> {
                //退出登录
                AccountManagerHelper.exitLogin()
                LoginActivity.startActivity(this)
                finishAfterTransition()
            }

            R.id.ov_about -> {
                //关于
                showToast("点击关于")
            }
        }
    }

    @Deprecated("Deprecated in Java")
    override fun onBackPressed() {
        if ((System.currentTimeMillis() - lastOnBackTime) < 2000) {
            finishAfterTransition()
        } else {
            lastOnBackTime = System.currentTimeMillis()
            showToast(R.string.click_once_exit)
        }
    }

    override fun onPermissionBean(permissionBean: MutableList<UserInfo.PermissionBean>) {
        mEntranceList.clear()
        mEntranceList.addAll(permissionBean)
        mAdapter.notifyDataSetChanged()
    }

    override fun onDestroy() {
        super.onDestroy()
        LogUtils.getInstance().shutdown()
    }
}